package com.wtwd.campus.receiver;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.annotation.MsgType;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.dao.CommandRecordMapper;
import com.wtwd.campus.dao.DeviceInfoMapper;
import com.wtwd.campus.entity.CommandRecord;
import com.wtwd.campus.entity.DeviceInfo;
import com.wtwd.campus.entity.MqGeneralValueResult;
import com.wtwd.campus.entity.MqMessage;
import com.wtwd.campus.h5_manager.entity.DeviceLog;
import com.wtwd.campus.h5_manager.service.DeviceLogService;
import com.wtwd.campus.utils.ReceiverUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author mjy
 * @date 2021/07/02
 * @description
 */
@Service
@MsgType(value = Constants.DEVICE_RESTART)
public class ReceiveRestartHandler implements IReceiverHandler {

    @Autowired
    private CommandRecordMapper commandRecordMapper;
    @Autowired
    private DeviceInfoMapper deviceInfoMapper;
    @Autowired
    private ReceiverUtils receiverUtils;
    @Autowired
    private DeviceLogService deviceLogService;

    @Override
    public void handlerMessage(MqMessage mqMessage) {
        MqGeneralValueResult mqGeneralValueResult = (MqGeneralValueResult) mqMessage.getObject();
        DeviceInfo deviceInfo = deviceInfoMapper.getDeviceInfoByImei(mqGeneralValueResult.getImei());
        if (deviceInfo != null) {
            //根据流水号，imei号，和指令类型找到唯一的一条指令记录(无需鉴别,根据流水号查出来的数据，确定就是这一条指令)
            CommandRecord commandRecord = commandRecordMapper.getCommandRecordByCondition2(mqGeneralValueResult.getImei(), mqGeneralValueResult.getCommandNumber(), Constants.DEVICE_RESTART);
            if (commandRecord != null) {
                //将指令结果改为成功
                commandRecordMapper.completeCommand(commandRecord.getId(), System.currentTimeMillis());
                //如果定位数据不为空的话
                if (mqGeneralValueResult.getMqLocationData() != null) {
                    if (mqGeneralValueResult.getMqLocationData().getLatitude() != 0 && mqGeneralValueResult.getMqLocationData().getLongitude() != 0) {
                        //进行定位数据存储以及围栏告警记录的存储
                        //处理指令回复的时间
                        String deviceDate = mqGeneralValueResult.getDeviceDate();
                        String deviceTime = mqGeneralValueResult.getDeviceTime();
                        Long time = getTime(deviceDate, deviceTime);
                        deviceInfo = receiverUtils.dealLocation(mqGeneralValueResult.getMqLocationData(), time, deviceInfo, 1);
                    }
                }
                deviceInfo.setLastUpTime(System.currentTimeMillis());
                deviceInfoMapper.updateDeviceInfo(deviceInfo);
                //删除缓存中的指令
                String delKey = Constants.DEVICE_RESTART.concat("-").concat(mqGeneralValueResult.getCommandNumber());
                receiverUtils.deleteCommandInRedis(mqGeneralValueResult.getImei(), delKey);
            }
            //存储设备接收日志
            DeviceLog deviceLog = new DeviceLog();
            deviceLog.setCommandNumber(mqGeneralValueResult.getCommandNumber());
            deviceLog.setSendUser(Constants.DEVICE_USER);
            deviceLog.setImei(mqGeneralValueResult.getImei());
            deviceLog.setBigType(Constants.MESS_TYPE);
            deviceLog.setType(Constants.DEVICE_RESTART);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("设备号", mqGeneralValueResult.getImei());
            jsonObject.put("时间", mqGeneralValueResult.getDeviceTime());
            jsonObject.put("经度", mqGeneralValueResult.getMqLocationData().getLongitude());
            jsonObject.put("纬度", mqGeneralValueResult.getMqLocationData().getLatitude());
            jsonObject.put("设备状态", mqGeneralValueResult.getDeviceStatus());
            deviceLog.setCmd(jsonObject.toJSONString());
            deviceLog.setSendTime(System.currentTimeMillis());
            deviceLog.setState(1);
            deviceLogService.addDeviceLog(deviceLog);
        } else {
            saveUnInStorage(mqGeneralValueResult.getImei());
            receiverUtils.cloaseChannel(mqGeneralValueResult.getImei());
        }
    }
}
